﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Teacher_ListStudent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["id"] != null)
        {
            //Lay id truyen tu querry string
            int id = Convert.ToInt32(Request.QueryString["id"].ToString());

            if (Session["WelcomeUser"] != null)
            {
                if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
                {
                    UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                    if (udm.createConnection() != null)
                    {
                        gvCourseInformation.DataSource = udm.showCoursesByID(udm.createConnection(), id);
                        if (!Page.IsPostBack)
                        {
                            gvCourseInformation.DataBind();
                        }

                        gvAdmission.DataSource = udm.showCoursesRegisByCourseID(udm.createConnection(),id);
                        if (!Page.IsPostBack)
                        {
                            gvAdmission.DataBind();
                        }
                        if (gvAdmission.Rows.Count == 0)
                        {
                            lblNone.Text = "(None)";
                        }
                    }
                    else
                    {
                        Response.Redirect("~/ErrorPage.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/LoginMessage.aspx?id=1");
                }
            }
            else
            {
                Response.Redirect("~/Login.aspx?id=2");
            } 
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void gvAdmission_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvAdmission.EditIndex = e.NewEditIndex;
        gvAdmission.DataBind();

        DropDownList ddl = (DropDownList)gvAdmission.Rows[e.NewEditIndex].Cells[1].Controls[1];

        ddl.Items.Add(new ListItem("Checking","0"));
        ddl.Items.Add(new ListItem("Accept", "1"));
        ddl.Items.Add(new ListItem("Reject", "2"));
    }
    protected void gvAdmission_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvAdmission.EditIndex = -1;
        gvAdmission.DataBind();
    }
    protected void gvAdmission_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int courseID = Convert.ToInt32(Request.QueryString["id"].ToString());
        string studentID = gvAdmission.Rows[e.RowIndex].Cells[4].Text.ToString();

        int lsID = Convert.ToInt32(gvAdmission.Rows[e.RowIndex].Cells[3].Text.ToString());

        DropDownList ddl = (DropDownList)gvAdmission.Rows[e.RowIndex].Cells[1].Controls[1];

        int status = Convert.ToInt32(ddl.SelectedValue.ToString());

        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (udm.createConnection() != null)
        {
            if (udm.updateRegisOfCourse(udm.createConnection(), lsID, status))
            {
                if (status == 1)
                {
                    if (!udm.addStudentOfCourse(udm.createConnection(), courseID, studentID))
                    {
                        Response.Redirect("~/ErrorPage.aspx");
                    }
                }

                gvAdmission.EditIndex = -1;
                gvAdmission.DataSource = udm.showCoursesRegisByCourseID(udm.createConnection(), courseID);
                gvAdmission.DataBind();
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }

        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
    protected void dvCourseInformation_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvCourseInformation.EditIndex = -1;
        gvCourseInformation.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvCourseInformation.EditIndex = e.NewEditIndex;
        gvCourseInformation.DataBind();
    }
    protected void dvCourseInformation_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int index = gvCourseInformation.EditIndex;
        CheckBox cb = (CheckBox)gvCourseInformation.Rows[index].Cells[15].Controls[0];

        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        int couresID = Convert.ToInt32(Request.QueryString["id"].ToString());

        if (cb.Checked)
        {
            udm.updateCourseRegis(udm.createConnection(), couresID, 1);
        }
        else
        {
            udm.updateCourseRegis(udm.createConnection(), couresID, 0);
        }

        gvCourseInformation.EditIndex = -1;
        gvCourseInformation.DataBind();

        gvCourseInformation.DataSource = udm.showCoursesByID(udm.createConnection(), couresID);
        gvCourseInformation.DataBind();
    }
}